import { Component, OnInit } from '@angular/core';
import { HostAppService } from '../api/hostApp';
import { ProfilesService } from '../services/profiles.service';

@Component({
    selector: 'app-sidebar',  // 确保这里是 'app-sidebar'
    templateUrl: './sidebar.component.pug',
    styleUrls: ['./sidebar.component.scss']
})
export class SidebarComponent implements OnInit {
    visible = false;
    profiles: any[] = [];

    constructor(
        private hostApp: HostAppService,
        private profilesService: ProfilesService,
    ) {}

    ngOnInit() {
        // 添加日志以便调试
        console.log('侧边栏组件初始化');
        
        // 监听侧边栏切换事件
        this.hostApp.sidebarToggle$.subscribe(() => {
            console.log('侧边栏切换事件接收到');
            this.visible = !this.visible;
            console.log('侧边栏可见性:', this.visible);
        });

        // 加载配置文件列表
        this.loadProfiles();
    }

    async loadProfiles() {
        try {
            this.profiles = await this.profilesService.getProfiles();
        } catch (error) {
            console.error('加载配置文件失败', error);
            this.profiles = [];
        }
    }

    launchProfile(profile: any) {
        this.profilesService.launchProfile(profile);
        this.visible = false; // 启动后关闭侧边栏
    }
}